//
//  BZUCDataBase.m
//  BZUCSDK
//
//  Created by 郭艺伟 on 2017/10/10.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "BZUCDataBase.h"


@interface BZUCDataBase()

@property (nonatomic, strong)FMDatabaseQueue *dbQueue;

@property (nonatomic, copy) NSString       *dbPath;

@end


@implementation BZUCDataBase

static BZUCDataBase  * dbManage = nil;

+ (instancetype)shareDataBase{
    
    static dispatch_once_t onceToken ;
    
    dispatch_once(&onceToken, ^{
        dbManage = [[super allocWithZone:NULL] init] ;
    }) ;
    
    return dbManage;
}


+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    
    return   [BZUCDataBase shareDataBase];
    
}

+ (id)copyWithZone:(struct _NSZone *)zone{
    
    return  [BZUCDataBase shareDataBase];
    
}

- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        [self createDataTable];
    }
    
    return  self;
}

- (void)createDataTable{
    
    NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    
    NSString *dbPath  = [NSString stringWithFormat:@"%@/%@",dirPath,@"zzf.db"];
    
    self.dbPath              = dbPath;
    
    FMDatabaseQueue *dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
    
    self.dbQueue             = dbQueue;
    
    NSLog(@"dbPath = %@",dbPath);
    
    NSString *userTable      = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'userTable' (customerId LONG LONG,username  TEXT,password TEXT,nickname TEXT,realName TEXT,phone TEXT,sectName TEXT,type INTEGER ,sectId INTEGER,status INTEGER,portrait TEXT,backgroundUrl TEXT ,sessionId TEXT,PRIMARY KEY (customerId))"];
    
    NSString *loginUserTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'loginUserTable' (customerId LONG LONG,loginAccount  TEXT,password TEXT,loginTime LONG ,expireTime LONG, PRIMARY KEY (customerId))"];
    
    NSString *userInfoTable      = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'userInfoTable' (customerId LONG LONG,portrait TEXT,nickname TEXT,customerNo TEXT,realName TEXT,sign TEXT,sex TEXT,birth TEXT,country INTEGER,provinceId INTEGER,cityId INTEGER,areaId LONG,address TEXT,qrCode TEXT,interest TEXT,profession TEXT,customerType TEXT,sectId LONG,sectName TEXT,avatarPath TEXT,qrPath TEXT,cityName TEXT,avatarLocal TEXT,identity TEXT,avatarLocalBig TEXT,PRIMARY KEY (customerId))"];
    
    NSArray *tables = @[userTable , loginUserTable, userInfoTable];
    
    [dbQueue inDatabase:^(FMDatabase *db) {
        
        [db beginTransaction];
        BOOL isRollBack  = NO;
        BOOL success     = NO;
        @try {
            for (NSString *table in tables) {
                if (![db tableExists:table]) {
                    success = [db executeUpdate:table];
                    if (!success) {
                        NSLog(@"创建%@表失败",table);
                    }else{
                        NSLog(@"创建%@表成功",table);
                    }
                }
            }
        } @catch (NSException *exception) {
            isRollBack = YES;
            [db rollback];
        } @finally {
            [db commit];
        }
    }];
}

- (FMDatabaseQueue *)getDatabaseQueue{
    
    return self.dbQueue;
}

- (NSString *)getDataBasePath{
    
    return self.dbPath;
}


@end
